<template>
  <div class="lifecycle-page">
    <h1>onBeforeMount 生命周期钩子</h1>
    
    <div class="lifecycle-card">
      <h2>什么是 onBeforeMount?</h2>
      <p>onBeforeMount 钩子在组件被挂载之前调用，此时模板已经编译完成，但还没有创建DOM节点。</p>
      
      <div class="code-example">
        <pre><code>
import { ref, onBeforeMount } from 'vue'

export default {
  setup() {
    const message = ref('初始消息')
    
    onBeforeMount(() => {
      console.log('组件挂载前')
      // 在这里可以进行一些初始化工作，但不能访问DOM
    })
    
    return { message }
  }
}
        </code></pre>
      </div>
    </div>
    
    <div class="lifecycle-card">
      <h2>实际演示</h2>
      <div class="demo-box">
        <p>DOM元素状态: {{ domStatus }}</p>
        <p>初始化时间: {{ initTime }}</p>
        <button @click="refreshComponent">刷新组件</button>
      </div>
    </div>
    
    <div class="lifecycle-card">
      <h2>适用场景</h2>
      <ul>
        <li><strong>数据预处理：</strong>在DOM渲染前对数据进行最后的处理</li>
        <li><strong>日志记录：</strong>记录组件即将挂载的日志</li>
        <li><strong>初始化不依赖DOM的资源：</strong>如WebSocket连接、事件监听器等</li>
      </ul>
    </div>
    
    <div class="lifecycle-card">
      <h2>企业实践</h2>
      <p>在实际开发中，onBeforeMount的使用场景：</p>
      <ul>
        <li>初始化第三方库，如图表库的配置（但不创建实例）</li>
        <li>在组件挂载前进行数据验证</li>
        <li>设置初始状态，如加载状态</li>
        <li>记录组件渲染性能的起始时间</li>
      </ul>
      <p>示例代码：</p>
      <div class="code-example">
        <pre><code>
import { ref, onBeforeMount } from 'vue'
import { initializeAnalytics } from '@/utils/analytics'

export default {
  setup() {
    const isLoading = ref(true)
    const renderStartTime = ref(0)
    
    onBeforeMount(() => {
      // 记录渲染开始时间
      renderStartTime.value = performance.now()
      
      // 初始化分析工具
      initializeAnalytics()
      
      // 数据验证和预处理
      validateUserPermissions()
    })
    
    return { isLoading }
  }
}
        </code></pre>
      </div>
    </div>
    
    <div class="navigation">
      <router-link to="/haha" class="nav-button">返回主页</router-link>
      <router-link to="/mounted" class="nav-button">下一个: onMounted</router-link>
    </div>
  </div>
</template>

<script>
import { ref, onBeforeMount } from 'vue'

export default {
  name: 'OnBeforeMountView',
  setup() {
    const domStatus = ref('未挂载')
    const initTime = ref('')
    
    onBeforeMount(() => {
      console.log('onBeforeMount 被调用')
      domStatus.value = '准备挂载，但DOM还不可用'
      initTime.value = new Date().toLocaleTimeString()
      
      // 尝试访问DOM元素（在onBeforeMount中不可用）
      try {
        const element = document.querySelector('.demo-box')
        console.log('DOM元素:', element) // 将为null
      } catch (error) {
        console.error('无法访问DOM:', error)
      }
    })
    
    const refreshComponent = () => {
      // 强制组件刷新
      domStatus.value = '刷新中...'
      setTimeout(() => {
        domStatus.value = '已刷新'
        initTime.value = new Date().toLocaleTimeString()
      }, 100)
    }
    
    return {
      domStatus,
      initTime,
      refreshComponent
    }
  }
}
</script>

<style scoped>
.lifecycle-page {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

h1 {
  color: #3498db;
  text-align: center;
  margin-bottom: 30px;
}

.lifecycle-card {
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

h2 {
  color: #2c3e50;
  margin-bottom: 15px;
}

.code-example {
  background-color: #2c3e50;
  color: #ecf0f1;
  padding: 15px;
  border-radius: 5px;
  overflow-x: auto;
  margin: 15px 0;
}

pre {
  margin: 0;
}

.demo-box {
  background-color: #e8f4fc;
  padding: 15px;
  border-radius: 5px;
  margin: 15px 0;
}

button {
  background-color: #3498db;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

button:hover {
  background-color: #2980b9;
}

ul {
  padding-left: 20px;
}

li {
  margin-bottom: 8px;
}

.navigation {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

.nav-button {
  display: inline-block;
  padding: 10px 20px;
  background-color: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 5px;
  transition: background-color 0.3s;
}

.nav-button:hover {
  background-color: #2980b9;
}
</style>